package gx.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

	/**
	 * yy-mm-dd HH:mm:SS格式的时间
	 * 
	 * @return String Date
	 */
	public static String getNowDate() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		String strTime = format.format( dateTime );
		// Debug.writeLog(strTime);
		return strTime;
	}


	/**
	 * yy-mm-dd HH:mm格式的时间
	 * 
	 * @return String Date
	 */
	public static String getNowDateMM() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
		String strTime = format.format( dateTime );
		// Debug.writeLog(strTime);
		return strTime;
	}


	/**
	 * yyyy-mm-dd hh:mm格式的时间
	 * 
	 * @return StringDate
	 */

	public static String getStringDate() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
		String strTime = format.format( dateTime );
		return strTime;
	}


	/**
	 * yy-mm-dd格式的时间
	 * 
	 * @return StringDate
	 */
	public static String getStringDateShort() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-M-d" );
		String strTime = format.format( dateTime );
		return strTime;
	}


	/**
	 * yy-mm-dd格式的时间
	 * 
	 * @return StringDate
	 */
	public static String getStringDateShort( Date dateTime ) {
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" );
		String strTime = format.format( dateTime );
		return strTime;
	}


	/**
	 * yy-mm-dd HH:mm格式的时间
	 * 
	 * @return String Date
	 */
	public static String getStringDateShort( long time ) {
		Date dateTime = new Date( time );
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		String strTime = format.format( dateTime );
		return strTime;
	}


	/**
	 * yy/mm/dd格式的时间
	 * 
	 * @return StringDate
	 */
	public static String formatDate( Date dateTime ) {
		SimpleDateFormat format = new SimpleDateFormat( "yyyy/MM/dd" );
		String strTime = format.format( dateTime );
		return strTime;
	}


	/**
	 * yy-mm格式的时间
	 * 
	 * @return StringDate
	 */
	public static String getStringDateYearMonth() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM" );
		String strTime = format.format( dateTime );
		return strTime;
	}


	/**
	 * yy格式的时间
	 * 
	 * @return StringDate
	 */
	public static String getStringDateYear() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat( "yyyy" );
		String strTime = format.format( dateTime );
		return strTime;
	}


	/**
	 * yy年mm月dd日 星期 格式的时间
	 * 
	 * @return StringDate
	 */
	public static String getStringNowDate() {
		Date dateTime = new Date();
		String strTime = DateFormat.getDateInstance( DateFormat.FULL ).format( dateTime );
		return strTime;
	}


	/**
	 * 格式化时间
	 * 
	 * @param datee
	 * @param newfmt
	 * @return
	 */
	public String FormatDateToString( Date datee, String newfmt ) {

		String re = "";
		if ( datee != null || newfmt != null || newfmt.equals( "" ) ) {

			SimpleDateFormat sdf = new SimpleDateFormat( newfmt );
			re = sdf.format( datee );
			return re;
		}
		else {
			return re;
		}

	}


	/**
	 * 指定格式
	 * 
	 * @param fmt
	 * @return
	 */
	public String getDate( String fmt ) {

		String re = null;
		if ( fmt != null || fmt.equals( "" ) ) {
			SimpleDateFormat sdf = new SimpleDateFormat( fmt );
			Date d = new Date( System.currentTimeMillis() );
			re = sdf.format( d );
			return re;
		}
		else {
			return re;
		}
	}


	public String dateFormat( String datee, String oldfmt, String newfmt ) {

		String re = null;
		if ( datee != null || datee.equals( "" ) || oldfmt != null || oldfmt.equals( "" ) || newfmt != null || newfmt.equals( "" ) ) {

			SimpleDateFormat sdf = new SimpleDateFormat( oldfmt );
			SimpleDateFormat sdf1 = new SimpleDateFormat( newfmt );

			try {
				Date d = sdf.parse( datee );
				re = sdf1.format( d );
				return re;
			}
			catch ( ParseException e ) {
				e.printStackTrace();
			}
		}
		else {

		}
		return re;
	}


	// 1-year 2-month 5-day 10-hour 12-minute 13-second 14-millisecond
	public String getTheDate( String time, String oldfmt, String newfmt, int i, int offset ) throws ParseException {

		DateUtil dt = new DateUtil();
		String value = "";
		if ( time != null && !time.equals( "" ) || oldfmt != null && !oldfmt.equals( "" ) || newfmt != null && !newfmt.equals( "" ) || ( i == 1 || i == 2 || i == 5 || i == 12 || i == 13 || i == 14 ) ) {

			SimpleDateFormat sdf = new SimpleDateFormat( oldfmt );
			SimpleDateFormat sdf1 = new SimpleDateFormat( newfmt );
			Date timedate = sdf.parse( time );

			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.setTime( timedate );

			cal.add( i, offset );

			value = sdf1.format( cal.getTime() );
		}

		return value;
	}


	/*
	 * 
	 */
	public int getDayNumberOfMonth( String day, String fmt ) {

		if ( ( day == null ) || ( day.trim().equals( "" ) || ( fmt == null ) || ( fmt.trim().equals( "" ) ) ) ) {
			return 0;
		}
		SimpleDateFormat sdf = new SimpleDateFormat( fmt );
		Date theday = null;
		try {
			theday = sdf.parse( day );
		}
		catch ( ParseException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		ca.setTime( theday );
		return ca.getActualMaximum( ca.DAY_OF_MONTH );
	}


	/*
	 * 
	
	 */
	public int getFirstDayOfWeek( String day, String fmt ) {

		if ( ( day == null ) || ( day.trim().equals( "" ) || ( fmt == null ) || ( fmt.trim().equals( "" ) ) ) ) {
			return 0;
		}
		SimpleDateFormat sdf = new SimpleDateFormat( fmt );
		Date theday = null;
		try {
			theday = sdf.parse( day );
		}
		catch ( ParseException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		ca.setFirstDayOfWeek( ca.MONDAY );
		ca.setTime( theday );
		ca.set( ca.DAY_OF_WEEK, ca.getFirstDayOfWeek() );

		return ca.getActualMaximum( ca.DAY_OF_MONTH );
	}


	/**
	 * 判断当前季度
	 * 
	 * @return
	 */
	public static String getQuarter() {
		String quarter = "";
		Calendar ca = Calendar.getInstance();
		int nowMonth = ca.get( Calendar.MONTH ) + 1;
		switch ( nowMonth ) {
			case 1:
			case 2:
			case 3:
				quarter = "1";
				break;
			case 4:
			case 5:
			case 6:
				quarter = "2";
				break;
			case 7:
			case 8:
			case 9:
				quarter = "3";
				break;
			case 10:
			case 11:
			case 12:
				quarter = "4";
		}
		return quarter;
	}


	/**
	 * 得到指定时间是周几
	 * 
	 * @param day
	 * @param fmt
	 * @return
	 */
	public int getDayIndexOfWeek( String day, String fmt ) {
		SimpleDateFormat sdf = new SimpleDateFormat( fmt );
		Date theday = null;
		try {
			theday = sdf.parse( day );
		}
		catch ( ParseException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		ca.setFirstDayOfWeek( Calendar.MONDAY );// 设置周一为每周第一天
		ca.set( Calendar.DAY_OF_WEEK, Calendar.SUNDAY );// 每周从周一开始
		ca.setTime( theday );
		return ca.get( 7 ) - 1;
	}


	/**
	 * 得到指定日期所在周的第一天（周一)
	 * 
	 * @param day
	 * @param fmt
	 * @return
	 */
	public static String getFirstDayOfWeek_Of_Day( String day, String fmt ) {

		SimpleDateFormat sdf = new SimpleDateFormat( fmt );
		Date theday = null;
		try {
			theday = sdf.parse( day );
		}
		catch ( ParseException e ) {
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		ca.setTime( theday );
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = ca.get( Calendar.DAY_OF_WEEK );// 获得当前日期是一个星期的第几天

		if ( 1 == dayWeek ) {
			ca.add( Calendar.DAY_OF_MONTH, -1 );
		}
		ca.setFirstDayOfWeek( Calendar.MONDAY );// 设置周一为每周第一天
		int weekNum = ca.get( Calendar.DAY_OF_WEEK );
		ca.add( Calendar.DATE, ca.getFirstDayOfWeek() - weekNum );

		return sdf.format( ca.getTime() );
	}


	/**
	 * 得到指定日期所在周的第最后一天（周日)
	 * 
	 * @param day
	 * @param fmt
	 * @return
	 */
	public static String getLastDayOfWeek_Of_Day( String day, String fmt ) {

		SimpleDateFormat sdf = new SimpleDateFormat( fmt );
		Date theday = null;
		try {
			theday = sdf.parse( day );
		}
		catch ( ParseException e ) {
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		ca.setTime( theday );
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = ca.get( Calendar.DAY_OF_WEEK );// 获得当前日期是一个星期的第几天

		if ( 1 == dayWeek ) {
			ca.add( Calendar.DAY_OF_MONTH, -1 );
		}
		ca.setFirstDayOfWeek( Calendar.MONDAY );// 设置周一为每周第一天
		int weekNum = ca.get( Calendar.DAY_OF_WEEK );
		ca.add( Calendar.DATE, ca.getFirstDayOfWeek() - weekNum );
		ca.add( Calendar.DATE, 6 );

		return sdf.format( ca.getTime() );
	}


	// 当前日期加减n天后的日期，返回String (yyyy-mm-dd)
	public static String nDaysAftertoday( int n ) {
		SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
		Calendar rightNow = Calendar.getInstance();
		// rightNow.add(Calendar.DAY_OF_MONTH,-1);
		rightNow.add( Calendar.DAY_OF_MONTH, +n );
		return df.format( rightNow.getTime() );
	}


	// 当前日期加减n天后的日期，返回String (yyyy-mm-dd)
	public static Date nDaysAfterNowDate( int n ) {
		Calendar rightNow = Calendar.getInstance();
		// rightNow.add(Calendar.DAY_OF_MONTH,-1);
		rightNow.add( Calendar.DAY_OF_MONTH, +n );
		return rightNow.getTime();
	}


	// 给定一个日期型字符串，返回加减n天后的日期型字符串
	public static String nDaysAfterOneDateString( String basicDate, int n ) {
		SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
		Date tmpDate = null;
		try {
			tmpDate = df.parse( basicDate );
		}
		catch ( Exception e ) {
			// 日期型字符串格式错误
			e.printStackTrace();
		}
		long nDay = ( tmpDate.getTime() / ( 24 * 60 * 60 * 1000 ) + 1 + n ) * ( 24 * 60 * 60 * 1000 );
		tmpDate.setTime( nDay );

		return df.format( tmpDate );
	}


	// 给定一个日期，返回加减n天后的日期
	public static Date nDaysAfterOneDate( Date basicDate, int n ) {
		long nDay = ( basicDate.getTime() / ( 24 * 60 * 60 * 1000 ) + 1 + n ) * ( 24 * 60 * 60 * 1000 );
		basicDate.setTime( nDay );

		return basicDate;
	}


	// 计算两个日期相隔的天数
	public static int nDaysBetweenTwoDate( Date firstDate, Date secondDate ) {
		int nDay = ( int ) ( ( secondDate.getTime() - firstDate.getTime() ) / ( 24 * 60 * 60 * 1000 ) );
		return nDay;
	}


	// 计算两个日期相隔的天数
	public static int nDaysBetweenTwoDate( String firstString, String secondString ) {
		SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
		Date firstDate = null;
		Date secondDate = null;
		try {
			firstDate = df.parse( firstString );
			secondDate = df.parse( secondString );
		}
		catch ( Exception e ) {
			// 日期型字符串格式错误
			e.printStackTrace();
		}

		int nDay = ( int ) ( ( secondDate.getTime() - firstDate.getTime() ) / ( 24 * 60 * 60 * 1000 ) );
		return nDay;
	}


	/**
	 * 得到指定日期月份最后一天
	 * 
	 * @param
	 */
	public static String monthLastDay( String date ) {

		String[] d = date.split( "-" );
		String[] ar = new String[ 12 ];
		ar[ 0 ] = "31";
		ar[ 1 ] = ( leapYear( d[ 0 ] ) ) ? "29" : "28";
		ar[ 2 ] = "31";
		ar[ 3 ] = "30";
		ar[ 4 ] = "31";
		ar[ 5 ] = "30";
		ar[ 6 ] = "31";
		ar[ 7 ] = "31";
		ar[ 8 ] = "30";
		ar[ 9 ] = "31";
		ar[ 10 ] = "30";
		ar[ 11 ] = "31";
		return ar[ Integer.parseInt( d[ 1 ] ) - 1 ];
	}


	/**
	 * 是否闰年
	 * 
	 * @return
	 */
	public static boolean leapYear( String yearint ) {

		int year = Integer.parseInt( yearint );
		if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) {
			return true;
		}
		return false;
	}


	public static void main( String[] args ) {

		System.out.println(getStringDateShort());
//		System.out.println( "周一：" + getFirstDayOfWeek_Of_Day( "2012-07-09", "yyyy-MM-dd" ) );
//		System.out.println( "周日：" + getLastDayOfWeek_Of_Day( "2012-07-09", "yyyy-MM-dd" ) );
		// DateUtil du =new DateUtil();
		//
		// int num = du.getDayIndexOfWeek("2010-05-07 10:12:45", "yyyy-MM-dd HH:mm:ss");
		// System.out.println(num);
		//
		// String fisrtdayOftheday = du.getFirstDayOfWeek_Of_Day("2010-05-07 10:12:45",
		// "yyyy-MM-dd HH:mm:ss");
		// System.out.println(fisrtdayOftheday);
		//
		// String lastdayOftheday = du.getLastDayOfWeek_Of_Day("2010-05-07 10:12:45",
		// "yyyy-MM-dd HH:mm:ss");
		// System.out.println(lastdayOftheday);
		//
		// String startRiqi = "2010-07-05";
		// String endRiqi = "2010-07-25";
		// List dates = new ArrayList();
		// int index=0;
		// while(!startRiqi.equalsIgnoreCase(endRiqi)){
		// Map map = new HashMap();
		// if(index==0){
		// map.put("fo"+index, startRiqi);
		// startRiqi = nDaysAfterOneDateString(startRiqi,6);
		// }else{
		// map.put("fo"+index, nDaysAfterOneDateString(startRiqi,1));
		// startRiqi = nDaysAfterOneDateString(startRiqi,7);
		// }
		// map.put("to"+index, startRiqi);
		// index=index+1;
		// dates.add(map);
		// }
		// //System.out.println(dates);
		//
		//
		// String currentDate = DateUtil.getStringDateShort();
		// String startDate =
		// DateUtil.getFirstDayOfWeek_Of_Day(currentDate,"yyyy-MM-dd");
		// String endDate = DateUtil.getLastDayOfWeek_Of_Day(currentDate,"yyyy-MM-dd");
		// System.out.println(startDate);
		// System.out.println(endDate);
	}


	/**
	 * 
	 * <summary></summary>
	 * 
	 * @return
	 * @author guoqiusheng
	 * @date 2013-11-19
	 */
	public static long getbetweenDays( String startDate, String endDate ) {
		long beginTime = new Date( startDate.replace( "-", "/" ) ).getTime();
		long endTime = new Date( endDate.replace( "-", "/" ) ).getTime();
		long betweenDays = ( long ) ( ( endTime - beginTime ) / ( 1000 * 60 * 60 * 24 ) );
		return betweenDays;
	}

}